using System;
using System.Xml;
using System.Collections.Specialized;

using RaisingStudio.Common.Xml;
using RaisingStudio.Data.Common.Configuration;

namespace RaisingStudio.Data.Common.Managers
{
	/// <summary>
	/// Summary description for ProviderDeSerializer.
	/// </summary>
	public sealed class ProviderDeSerializer
	{
        /// <summary>
        /// Deserializes the specified node in a <see cref="IDbProvider"/>.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns>The <see cref="IDbProvider"/></returns>
		public static IDbProvider Deserialize(XmlNode node)
		{
			IDbProvider provider = new DbProvider();
			NameValueCollection prop = NodeUtils.ParseAttributes(node);

			provider.AssemblyName = prop["assemblyName"];
			provider.CommandBuilderClass = prop["commandBuilderClass"];
			provider.DbCommandClass = prop["commandClass"];
			provider.DbConnectionClass = prop["connectionClass"];
			provider.DataAdapterClass = prop["dataAdapterClass"];
			provider.Description = prop["description"];
			provider.IsDefault = NodeUtils.GetBooleanAttribute(prop, "default", false);
			provider.IsEnabled = NodeUtils.GetBooleanAttribute(prop, "enabled", true);
			provider.Name = prop["name"];
			provider.ParameterDbTypeClass = prop["parameterDbTypeClass"];
			provider.ParameterDbTypeProperty = prop["parameterDbTypeProperty"];
			provider.ParameterPrefix = prop["parameterPrefix"];
			provider.SetDbParameterPrecision = NodeUtils.GetBooleanAttribute(prop, "setDbParameterPrecision", true);
			provider.SetDbParameterScale = NodeUtils.GetBooleanAttribute(prop, "setDbParameterScale", true);
			provider.SetDbParameterSize = NodeUtils.GetBooleanAttribute(prop, "setDbParameterSize", true);
			provider.UseDeriveParameters = NodeUtils.GetBooleanAttribute(prop, "useDeriveParameters", true);
			provider.UseParameterPrefixInParameter = NodeUtils.GetBooleanAttribute(prop, "useParameterPrefixInParameter", true);
			provider.UseParameterPrefixInSql = NodeUtils.GetBooleanAttribute(prop, "useParameterPrefixInSql", true);
			provider.UsePositionalParameters = NodeUtils.GetBooleanAttribute(prop, "usePositionalParameters", false);
            provider.AllowMARS = NodeUtils.GetBooleanAttribute(prop, "allowMARS", false);

			return provider;
		}
	}
}
